package com.kfm.jdbc.day0327;

import java.sql.*;
import java.util.Properties;

public class Demo {

    public static void main(String[] args) {
        try {
            // 1. 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 获取连接
            String url = "jdbc:mysql://localhost:3306/kfm_store";
            String username = "root";
            String password = "";
            Connection connection = DriverManager.getConnection(url, username, password);

            // 3. 预处理 SQL
            String sql ="select * from account where id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            // 4. 设置值
            preparedStatement.setObject(1, 1);

            // 5. 执行 SQL
            ResultSet resultSet = preparedStatement.executeQuery();

            // 6. 处理结果集
            if (resultSet.next()) {
                System.out.println("查询到结果了");
                System.out.println(resultSet.getObject(1));
                System.out.println(resultSet.getObject(2));
            }

            // 7. 释放资源
            resultSet.close();
            preparedStatement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
